package org.example.user.controller;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.example.user.domain.Bankcard;
import org.example.user.domain.Transfers;
import org.example.user.domain.User;
import org.example.user.service.BankcardService;
import org.example.user.service.TransfersService;
import org.example.user.service.UserService;
import org.example.user.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;


@RequestMapping("/account")
@RestController
public class AccountController {
    @Autowired
    private UserService userService;
    @Autowired
    private BankcardService bankcardService;
    @Autowired
    private TransfersService transfersService;

    //转账给我的好友
    @RequestMapping("transferMyFriend")
    public Result transferMyFriend(@RequestBody Transfers transfers, HttpServletRequest request) {
        return Result.success();
    }

    @RequestMapping("seLimit")
    public String seLimit() {
        List<Bankcard> list = bankcardService.list();
        System.out.println(list + "////////////////////////////////////////" + "\n");
        for (Bankcard bankcard : list) {
            if (bankcard.getTransferLimit() != null) {
                bankcard.setTransferLimit(new BigDecimal(10000));
                boolean b = bankcardService.updateById(bankcard);
            }
            System.out.println(bankcard.getBankAccount() + bankcard.getTransferLimit() + "\n");
        }

        return "刷新每日银行卡限制额度";
    }
    //全部提现
//    @RequestMapping("allDraw")
//    public Result allDraw(@RequestBody Transfers transfers, HttpServletRequest request) {
//        String token = request.getHeader("token");
//        JWT jwt = JWTUtil.parseToken(token);
//        Integer id = (Integer) jwt.getPayload("id");
//        User userDB = userService.getById(id);
//
//        Integer bankid = transfers.getPayerBankid();
//        LambdaQueryWrapper<Bankcard> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(Bankcard::getUid, id);
//        wrapper.eq(Bankcard::getBankType, bankid);
//        Bankcard one = bankcardService.getOne(wrapper);
//        if (!one.getBankAccount().equals(transfers.getBankAccount())) {
//            return Result.fail("银行账号有误");
//        }
//
//
//        boolean b = transfersService.save2(transfers, userDB, one);
//
//
//        return b ? Result.success() : Result.fail();
//    }

    @RequestMapping("/list")
    public Result list(String token) {
        JWT jwt = JWTUtil.parseToken(token);
        Integer userId = (Integer) jwt.getPayload("id");
        User user = userService.getById(userId);
        return Result.success(user);
    }

    @RequestMapping("/topUp")
    public Result topUp(@RequestBody Transfers transfers, HttpServletRequest request) {
        String token = request.getHeader("token");
        JWT jwt = JWTUtil.parseToken(token);
        Integer userId = (Integer) jwt.getPayload("id");
        User userDB = userService.getById(userId);
        System.out.println(userDB);
//        获取付款人银行卡类型id
        Integer bankId = transfers.getPayerBankid();
//        根据 用户人+银行卡类型id获取准确数据
        LambdaQueryWrapper<Bankcard> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Bankcard::getUid, userId);
        wrapper.eq(Bankcard::getBankType, bankId);
        Bankcard bankcard = bankcardService.getOne(wrapper);
        boolean b = false;
//        根据查询出来的银行卡信息校验 银行卡账号是否有误
        if (!bankcard.getBankAccount().equals(transfers.getBankAccount())) {
            return Result.fail("银行账号错误请重新输入");
        }
        if (bankcard.getBalance().compareTo(transfers.getTransferAmount()) < 0) {
            return Result.fail("银行卡余额不足，无法支付");
        }
        if (bankcard.getTransferLimit().compareTo(transfers.getTransferAmount()) < 0) {
            return Result.fail("银行卡今日额度已达上限");
        }

        b = transfersService.save1(transfers, userDB, bankcard);

        return b ? Result.success("充值成功") : Result.fail();
    }

    //提现
    @RequestMapping("/withDraw")
    public Result withdraw(@RequestBody Transfers transfers, HttpServletRequest request) {
        String token = request.getHeader("token");
        JWT jwt = JWTUtil.parseToken(token);
        Integer id = (Integer) jwt.getPayload("id");
        User userDB = userService.getById(id);
//        用户零钱
        BigDecimal money = userDB.getMoney();

        Integer bankid = transfers.getPayerBankid();
        LambdaQueryWrapper<Bankcard> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Bankcard::getUid, id);
        wrapper.eq(Bankcard::getBankType, bankid);
        Bankcard one = bankcardService.getOne(wrapper);
        if (!one.getBankAccount().equals(transfers.getBankAccount())) {
            return Result.fail("银行账号有误");
        }
        if (money.compareTo(transfers.getTransferAmount()) < 0) {
            return Result.fail("钱包余额不足,无法提现");
        }
        if (transfers.getTransferAmount().equals(new BigDecimal(0))) {
            return Result.fail("钱包余额为零，请及时充值。");
        }

        boolean b = transfersService.save2(transfers, userDB, one);


        return b ? Result.success() : Result.fail();
    }
}
